package com.xlh.service.courseTest.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.courseTest.AnswerSheetReportMapper;
import com.xlh.dao.courseTest.AnswerSheetReportMapperExt;
import com.xlh.dao.courseTest.ExperimentReportMapperExt;
import com.xlh.pojo.courseTest.AnswerSheetReport;
import com.xlh.pojo.courseTest.AnswerSheetReportExample;
import com.xlh.service.courseTest.AnswerSheetExperimentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhr 2019/4/17
 */
@Service
public class AnswerSheetExperimentServiceImpl implements AnswerSheetExperimentService {

    @Autowired
    private AnswerSheetReportMapper mapper;
    @Autowired
    private AnswerSheetReportMapperExt mapperExt;
    @Autowired
    private ExperimentReportMapperExt experimentReportMapperExt;

    @Override
    public Long getReportId(Long answerSheetId, Long questionId) {
        Long id = null;
        AnswerSheetReportExample example = new AnswerSheetReportExample();
        example.createCriteria().andAnswerSheetIdEqualTo(answerSheetId).andQuestionIdEqualTo(questionId);
        List<AnswerSheetReport> reportList = mapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(reportList)) {
            id = reportList.get(0).getReportId();
        }
        return id;
    }

    @Override
    public void insert(Long reportId, Long answerSheetId, Long questionId) {
        AnswerSheetReport report = new AnswerSheetReport(null, answerSheetId, questionId, reportId);
        mapper.insert(report);
    }

    @Override
    public List<AnswerSheetReport> selectByQuestionIds(List<Long> questionIds) {
        if (CollectionUtils.isEmpty(questionIds)) {
            return Lists.newArrayList();
        }
        AnswerSheetReportExample example = new AnswerSheetReportExample();
        example.createCriteria().andQuestionIdIn(questionIds);
        List<AnswerSheetReport> result = mapper.selectByExample(example);
        return CollectionUtils.isEmpty(result) ? Lists.newArrayList() : result;
    }

    @Override
    public void cleanData() {
        mapperExt.cleanData();

        experimentReportMapperExt.cleanPaperData();
    }
}
